package com.t.bricks.bussiness.db.dao.voterecord.impl;

import java.util.Arrays;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import com.t.bricks.bussiness.db.dao.voterecord.VoteRecordDao;
import com.t.bricks.bussiness.db.entity.voterecord.VoteRecord;
import com.t.bricks.bussiness.db.mapper.voterecord.VoteRecordMapper;
import com.t.bricks.model.Log;
import com.t.bricks.model.MsgEmity;

@Repository
public class VoteRecordDaoImpl implements VoteRecordDao<VoteRecord> {
	
	@Autowired
	private VoteRecordMapper voteRecordMapper;

	@Value("${app.findPage.dLikeDate:配置文件中的当前库全文检索时间范围dLikeDate未设置}")
	private String sLikeDate;

	/**
	 * 添加
	 * @param voteRecord
	 * @return MsgEmity
	 */
	@Transactional
	@Override
	public MsgEmity add(VoteRecord voteRecord) {
		try {
			int i = voteRecordMapper.add(voteRecord);
			if (i < 1) {
				Log.error("新增未能得到预期影响值(预期1):", i);
				return new MsgEmity(false, "新增数据失败！", 7001);
			}
			
			i = voteRecordMapper.addVoteStatistics(voteRecord.getsId());
			if (i < 1) {
				Log.error("新增对应的投票统计数据记录时未能得到预期影响值(预期1):", i);
				TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();//手动回滚事务
				return new MsgEmity(false, "新增对应的投票统计数据记录失败！", 7003);
			}
		} catch (Exception e) {
			Log.error("新增时发生异常:", e.getMessage());
			TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();//手动回滚事务
			return new MsgEmity(false, "新增失败！", 7002);
		}
		
		return new MsgEmity(true, "新增成功！", voteRecord);
	}

	/**
	 * 检查待新增内容是否存在重复数据
	 * @param voteRecordData
	 * @return MsgEmity
	 */
	@Override
	public MsgEmity checkRepeatByAdd(VoteRecord voteRecordData) {
		int i = 0;
		try {
			i = voteRecordMapper.checkRepeatByAdd(voteRecordData);
			if (0 == i) {
				return new MsgEmity(false, "查询成功！没发现重复", 7007);
			}
		} catch (Exception e) {
			Log.error("检查待新增内容是否存在重复数据发生异常:", e.getMessage());
			return new MsgEmity(false, "查询失败，请查看异常日志", 7008);
		}
		
		return new MsgEmity(true, "查询成功！发现重复", i);//将查询到的数量(或sql中的构造码)返回以便特殊业务要求
	}

	/**
	 * 修改状态
	 * @param sId 编号
	 * @param iState 状态值
	 * @param iVersion 记录版本号
	 * @param sMemo 备注
	 * @return MsgEmity 返回执行情况
	 */
	@Transactional
	@Override
	public MsgEmity changeState(String sId, Integer iState, Integer iVersion, String sMemo) {
		try {
			int i = voteRecordMapper.changeState(sId, iState, iVersion, sMemo);
			if (i < 1) {
				MsgEmity me = this.getValueByFieldName(sId, Arrays.asList("iState", "iVersion"), false);//如果影响数小于1,则检查数据
				if (!me.isSuccess()) {
					return me.appendDataCode(100);
				}
				
				Map<String, Object> map = me.getData();
				if (null == map) {
					return new MsgEmity(false, "没有找到对应数据！", 7001);//删除影响数失败,但数据也不存在则视为成功
				}
				
				if (iVersion != Integer.valueOf(map.get("iVersion").toString()).intValue()) {
					return new MsgEmity(false, "更新状态失败，系统中的数据可能已经被更新！", 7002);
				}
				
				if (iState >= 0 && iState < Integer.valueOf(map.get("iState").toString()).intValue()) {
					return new MsgEmity(false, "更新状态失败，状态禁止逆操作！", 7003);
				}

				Log.error("更新状态未能得到预期影响值(预期1):", i);
				return new MsgEmity(false, "更新状态数据失败！", 7004);
			}
			
			i = voteRecordMapper.changeStateStatistics(sId, iState);//按投票记录修改'投票统计'记录状态
			if (i < 1) {
				Log.error("更新对应'投票统计'记录状态时失败");
				TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();//手动回滚事务
				return new MsgEmity(false, "更新对应'投票统计'记录状态时失败！", 7006);
			}
		} catch (Exception e) {
			Log.error("更新状态时发生异常:", e.getMessage());
			TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();//手动回滚事务
			return new MsgEmity(false, "更新状态失败！", 7005);
		}
		
		return new MsgEmity(true, "更新状态成功！", 7999);
	}

	//---------------------------- 自动生成代码结束 ----------------------------//

}
